<?php

namespace app\zucai\home;

use app\index\controller\Home;
use think\Db;

/**
 * zucai 前台模块
 */
class Index extends Home
{
    //首页公告接口
    public function notice()
    {
        $data = Db::name('notice')->find();
        return returnOk($data['content']);
    }

    //首页展示图接口
    public function image()
    {
        $data = Db::name('image')->where('id', 1)->select();
        foreach ($data as $k => $v) {
            $data[$k]['path'] = get_file_path($v['path']);
        }
        return returnOk($data);
    }

    //首页搜索接口
    public function search(){
        if($this->request->isPost()){
            if($this->request->post()){
                $post=$this->request->post();
                if (!(isset($post['name']) && $post['name'])) return returnError('未获取到name');
                if (!(isset($post['sign']) && $post['sign'])) return returnError('未获取到sign');

                $admin_id=Db::name('admin_user')->where('sign',$post['sign'])->value('id');
                if (!$admin_id) return returnError('违规操作');

                $con= Db::name('user')->field('id,name,avatar,content')
                    ->where('status',1)
                    ->where('uid',$admin_id)
//                   ->where('user','like','%'.$post['name'].'%')
                    ->where('name','like','%'.$post['name'].'%')->select();
                if($con){
                    foreach ($con as $k=>$v) {
                        if ($v['avatar']>=1&&$v['avatar']<=11) $con[$k]['avatar']=get_file_path($v['avatar']);
                    }

                    return returnOk($con);
                }else{
                    return returnError('该用户不存在！');
                }
            }else{
                return returnError('未获取到参数！');
            }
        }else{
            return returnError('请使用POST方式请求');
        }
    }

    /**
     * @return \think\response\Json
     * 获取文章列表
     * gxq <know1111@vip.qq.com>
     */
    public function article()
    {
        $article = Db::name('article')->where('status', 1)->order('create_time desc')->field('status',true)->select();

        foreach ($article as $k => $v) {
            $article[$k]['img'] = get_file_path($v['img']);
        }
        return returnOk($article);
    }


    /**
     * @return \think\response\Json
     * 获取彩种列表
     * gxq <know1111@vip.qq.com>
     */
    public function caizhong()
    {
        $caizhong = Db::name('caizhong')->where('status', 1)->field('status',true)->select();

        foreach ($caizhong as $k => $v) {
            $caizhong[$k]['logo'] = get_file_path($v['logo']);
        }
        return returnOk($caizhong);
    }

    /**
     * @return \think\response\Json
     * 推荐发单人
     * gxq <know1111@vip.qq.com>
     */
    public function recommender()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['sign']) && $post['sign'])) return returnError('未获取到sign');

        $sign = trimall($post['sign']);//签名

        $admin_id=Db::name('admin_user')->where('sign',$sign)->value('id');

        if (!$admin_id) return returnError('违规操作');

        $userinfo=Db::name('order')

            ->alias('b')
            ->join('zc_user a','a.id=b.uid')
            ->where('a.uid',$admin_id)
            ->where('isdraw',2)
            ->where(' DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(oddtime)')
            ->limit(8)
            ->group('b.uid')
            ->field('a.id,a.avatar,a.name,count(b.id) as count')
            ->order('count desc')
            ->select();

        foreach ($userinfo as $k=>$v) {
            if ($v['avatar']>=1&&$v['avatar']<=11){
            $userinfo[$k]['avatar']=get_file_path($v['avatar']);
            }
        }

        return returnOk($userinfo);
    }
    
}